package com.cooper.resource.download;

import java.util.Collection;

import com.cooper.resource.download.DownloadManagerService.DownloadBinder;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.ListFragment;

public abstract class DownloadingFragment extends ListFragment implements
		DownloadControl {

	protected static final String ADD_DOWN_PARAM_NAME = "ADD_DOWN_PARAM_NAME";

	protected abstract void onCompleteDown(DownloadStatus res);

	protected abstract void onItemCountChange(int count);

	protected abstract void onItemCancel(DownloadStatus res);

	protected abstract void onCancelAll();

	protected boolean completeDown(DownloadStatus res) {
		if (((DownloadServiceMgr) getActivity()).getDownloadServerBinder()
				.completeMove(res)) {
			adapter.removeItem(res);
			onItemCountChange(adapter.getCount());
			return true;
		}
		return false;
	}

	protected void doItemCancel(DownloadStatus res) {
		((DownloadServiceMgr) getActivity()).getDownloadServerBinder().cancel(
				res);
		adapter.removeItem(res);
		onItemCountChange(adapter.getCount());
	}

	protected void doCancelAllItem() {
		((DownloadServiceMgr) getActivity()).getDownloadServerBinder()
				.cancelAll();
		adapter.removeAll();
		onItemCountChange(adapter.getCount());
	}

	@Override
	public void pause(DownloadStatus res) {
		((DownloadServiceMgr) getActivity()).getDownloadServerBinder().pause(
				res);
	}

	@Override
	public void pauseAll() {
		((DownloadServiceMgr) getActivity()).getDownloadServerBinder()
				.pauseAll();
	}

	@Override
	public void continueAll() {
		((DownloadServiceMgr) getActivity()).getDownloadServerBinder()
				.continueAll();
	}

	@Override
	public void cancelAll() {
		onCancelAll();
	}

	@Override
	public void downContinue(DownloadStatus res) {
		((DownloadServiceMgr) getActivity()).getDownloadServerBinder()
				.downContinue(res);
	}

	@Override
	public void cancel(DownloadStatus res) {
		onItemCancel(res);
	}

	@Override
	public boolean completeMove(DownloadStatus res) {
		if (res.getDownloadResource().isFinish()) {
			onCompleteDown(res);
		}
		return false;
	}

	@Override
	public Collection<DownloadStatus> getDownloading() {
		DownloadBinder binder = ((DownloadServiceMgr) getActivity())
				.getDownloadServerBinder();
		if (binder == null) {
			return null;
		} else
			return binder.getDownloading();
	}

	private DownloadItemAdapter adapter;
	
	private Handler handle = new Handler();
	
	private void postDownloadListener(){
		DownloadBinder binder = ((DownloadServiceMgr) getActivity()).getDownloadServerBinder();
		if (binder == null){
			handle.postDelayed(new Runnable(){

				@Override
				public void run() {
					postDownloadListener();
					
				}}, 1000);
		}else{
			binder.setDownloadListener(adapter);
		}
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		adapter = new DownloadItemAdapter(getActivity(),
				DownloadingFragment.this);
		setListAdapter(adapter);
		postDownloadListener();

		/*
		 * if (getArguments() != null) { Serializable downData =
		 * getArguments().getSerializable( ADD_DOWN_PARAM_NAME); if (downData !=
		 * null) {
		 * 
		 * DownloadResource requestRes = (DownloadResource) downData;
		 * getArguments().remove(ADD_DOWN_PARAM_NAME);
		 * 
		 * addDownload(requestRes);
		 * 
		 * } }
		 */
	}

	/*
	 * 
	 * private void addDownload(DownloadResource requestRes) {
	 * 
	 * Log.d("DownloadingActivity", requestRes.getDownloadTitle()); File
	 * storeDir = requestRes.getDownloadingSaveDir(); if (storeDir.exists()) {
	 * Log.d("DownloadingActivity", "Down is exists"); if
	 * (!((DownloadServiceMgr) getActivity()).getDownloadServerBinder()
	 * .downloadPrefIsExists(storeDir)) { FileUtils.delete(storeDir); } else {
	 * Toast.makeText(getActivity(), R.string.download_task_exists,
	 * Toast.LENGTH_LONG).show(); return; } }
	 * adapter.addDownload(((DownloadServiceMgr) getActivity())
	 * .getDownloadServerBinder().addDownload(requestRes));
	 * onItemCountChange(adapter.getCount());
	 * 
	 * }
	 */
	@Override
	public void onDestroy() {
		super.onDestroy();
	}

}
